package pub.tbc.api.config;

import lombok.extern.slf4j.Slf4j;
import pub.tbc.toolkit.core.CastUtil;
import pub.tbc.toolkit.core.EmptyUtil;
import pub.tbc.toolkit.core.collect.Lists;

import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * @author tbc on 2017/3/24 11:38:16.
 */
@Slf4j
public class PropertiesConfig implements Config {
    private Properties p;

    PropertiesConfig(Properties properties) {
        this.p = properties;
    }

    public Properties getProperties() {
        return p;
    }

    /**
     * 带默认值的get方法统一处理
     *
     * @param key          key
     * @param defaultValue defaultValue
     * @param v            v
     * @param <T>          type
     * @return T
     */
    private <T> T proDefaultValue(String key, T defaultValue, T v) {
        String value = get(key);
        if (EmptyUtil.isEmpty(value)) {
            return defaultValue;
        }
        return v;
    }

    public Set<String> keySet() {
        return p.keySet().stream().map(CastUtil::castString).collect(Collectors.toSet());
    }

    public void forEachByKey(Consumer<String> bi) {
        keySet().forEach(bi);
    }

    public void forEach(BiConsumer<Object, Object> biConsumer) {
        p.forEach(biConsumer);
    }

    @Override
    public String get(String key) {
        return p.getProperty(key);
    }

    @Override
    public String get(String key, String defaultValue) {
        return EmptyUtil.isEmpty(p.getProperty(key)) ? defaultValue : p.getProperty(key);
    }

    @Override
    public List<String> getList(String key, String separate) {
        String value = get(key);
        if (EmptyUtil.isEmpty(value))
            throw new RuntimeException("该配置项为空，无法分割为集合");
        return Lists.newArrayList(value.split(separate));
    }

    @Override
    public int getInt(String key) {
        return Integer.parseInt(get(key));
    }

    @Override
    public int getInt(String key, int defaultValue) {
        return proDefaultValue(key, defaultValue, getInt(key));
    }

    @Override
    public long getLong(String key) {
        return Long.parseLong(get(key));
    }


    @Override
    public long getLong(String key, long defaultValue) {
        return proDefaultValue(key, defaultValue, getLong(key));
    }

    @Override
    public boolean getBoolean(String key) {
        return Boolean.parseBoolean(get(key));
    }

    @Override
    public boolean getBoolean(String key, boolean defaultValue) {
        return proDefaultValue(key, defaultValue, getBoolean(key));
    }

    @Override
    public float getFloat(String key) {
        return Float.parseFloat(get(key));
    }

    @Override
    public float getFloat(String key, float defaultValue) {
        return proDefaultValue(key, defaultValue, getFloat(key));
    }

    @Override
    public double getDouble(String key) {
        return Double.parseDouble(get(key));
    }

    @Override
    public double getDouble(String key, double defaultValue) {
        return proDefaultValue(key, defaultValue, getDouble(key));
    }

}
